package com.lenovo.chartdemo;

import android.content.Context;
import android.graphics.Color;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.YAxisValueFormatter;

import java.util.ArrayList;

/*********************************************************************************
 Created by Android Studio.
 *Author:          Jack Fu
 *Version:         1.0
 *Date;            17-5-29 上午11:12
 *Description:     
 **********************************************************************************/
public class BarChartUtil {
    public static BarChart getChart(Context context){
        BarChart mChart = new BarChart(context);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);

        mChart.setDescription("");
        mChart.setMaxVisibleValueCount(10);
        mChart.setPinchZoom(false);
        mChart.setDrawGridBackground(false);

        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setSpaceBetweenLabels(2);


        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setLabelCount(8, false);
        leftAxis.setSpaceTop(15f);

        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setEnabled(false);

        mChart.getLegend().setEnabled(false);

        setData(60000,mChart);
        return mChart;
    }

    private static void setData(float range,BarChart mChart) {
        int colors1[] = new int[2];
        int colors2[] = new int[2];
        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < 2; i++) {
            xVals.add((i+1)+" Bus");
        }

        ArrayList<BarEntry> station1 = new ArrayList<BarEntry>();
        for (int i = 0; i < 2; i++) {
            float mult = (range + 1);
            float val = (float) (Math.random() * mult);
            station1.add(new BarEntry(val, i));
        }

        ArrayList<BarEntry> station2 = new ArrayList<BarEntry>();
        for (int i = 0; i < 2; i++) {
            float mult = (range + 1);
            float val = (float) (Math.random() * mult);
            station2.add(new BarEntry(val, i));
        }

        for (int i = 0; i < station1.size(); i++) {
            if(station1.get(i).getVal() > station2.get(i).getVal()){
                colors1[i] = Color.YELLOW;
                colors2[i] = Color.GREEN;
            }else{
                colors2[i] = Color.YELLOW;
                colors1[i] = Color.GREEN;
            }
        }

        BarDataSet set1 = new BarDataSet(station1, "DataSet");
        set1.setColors(colors1);
        set1.setBarSpacePercent(35f);

        BarDataSet set2 = new BarDataSet(station2, "DataSet");
        set2.setColors(colors2);
        set2.setBarSpacePercent(35f);

        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
        dataSets.add(set1);
        dataSets.add(set2);

        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);

        mChart.setData(data);
    }


}
